package com.css.fghs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.constant.Result;
import com.css.fghs.entity.domain.FgjyYhzxYhkxxbDO;
import com.css.fghs.entity.vo.YhkxxbVO;
import com.css.fghs.service.FgjyYhzxYhkxxbService;
import com.css.session.SessionUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "账户中心")
@Slf4j
@RestController
@RequestMapping("/zh")
public class ZhController {

    @Resource
    private FgjyYhzxYhkxxbService yhkxxbService;

    @PostMapping("/addYhkxx")
    public Result addYhkxx(@RequestBody YhkxxbVO yhkxxbVO) {
        FgjyYhzxYhkxxbDO yhkxxbDO = new FgjyYhzxYhkxxbDO();
        BeanUtils.copyProperties(yhkxxbVO, yhkxxbDO);
        yhkxxbDO.setYhuuid(SessionUtils.getYhuuid());
        //todo 根据银行卡号判断银行
        yhkxxbDO.setBankName("xx银行");
        yhkxxbDO.setXybz("N");
        yhkxxbDO.setLrrq(new Date());
        yhkxxbDO.setLrrsfid(SessionUtils.getYhuuid());
        yhkxxbService.save(yhkxxbDO);
        return Result.ok();
    }

    @PostMapping("/getYhkxxPage")
    public Result<Page<FgjyYhzxYhkxxbDO>> getYhkxxPage(@RequestBody YhkxxbVO yhkxxbVO) {
        return Result.ok(yhkxxbService.getYhkxxPage(yhkxxbVO));
    }

    @GetMapping("/delYhkxx")
    public Result delYhkxx(@RequestParam String uuid) {
        yhkxxbService.removeById(uuid);
        return Result.ok();
    }

    @GetMapping("/setYhkxxXy")
    public Result setYhkxxXy(@RequestParam String uuid) {
        FgjyYhzxYhkxxbDO yhkxxbDO = new FgjyYhzxYhkxxbDO();
        yhkxxbDO.setUuid(uuid);
        yhkxxbDO.setXybz("Y");
        yhkxxbDO.setXgrq(new Date());
        yhkxxbService.updateById(yhkxxbDO);

        LambdaQueryWrapper<FgjyYhzxYhkxxbDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FgjyYhzxYhkxxbDO::getYhuuid, SessionUtils.getYhuuid());
        queryWrapper.ne(FgjyYhzxYhkxxbDO::getUuid,uuid);
        queryWrapper.eq(FgjyYhzxYhkxxbDO::getXybz,"Y");

        FgjyYhzxYhkxxbDO udpyhkxxbDO = new FgjyYhzxYhkxxbDO();
        udpyhkxxbDO.setXybz("N");
        yhkxxbDO.setXgrq(new Date());
        yhkxxbService.update(udpyhkxxbDO, queryWrapper);
        return Result.ok();
    }
}
